﻿using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class Weapon : Item
{
    public enum Level {
        Small,
        Middle,
        Big
    }
    public int fitSlotTypeID { get { return m_FitSlotTypeID; } }
    public Vector2 pivot { get { return m_Pivot; } set { m_Pivot = value; } }
    public List<FlyItem> flyItems { get { return m_FlyItems; } }
    public float turnSpeed{set{ m_TurnSpeed = value; } get{ return m_TurnSpeed; } }
    public Level level { get { return m_Level; } set { m_Level = value; } }
    public Level m_Level;
    public float GetShotingOffsetRandom { get {

            float shotingOffset = UnityEngine.Random.Range(-m_ShotingOffset, m_ShotingOffset);
            m_ShotingOffset += shotingOffsetVelocity;
            m_ShotingOffset = Math.Min(shotingOffsetMax, m_ShotingOffset);
            return shotingOffset;
        } }
    public float shotingOffset { set { m_ShotingOffset = value; } get { return m_ShotingOffset; } }
    public float shotingOffsetMax { set { m_ShotingOffsetMax = value; } get { return m_ShotingOffsetMax; } }
    public float shotingOffsetVelocity { set { m_ShotingOffsetVelocity = value; } get { return m_ShotingOffsetVelocity; } }
    float m_ShotingOffset = 0;
    float m_ShotingOffsetMax = 12;
    float m_ShotingOffsetVelocity = 1.5f;
    int m_FitSlotTypeID = (int)ItemType.WEAPON_SLOT;
    Vector2 m_Pivot = new Vector2(0.5f, 0.5f);
    List<FlyItem> m_FlyItems = new List<FlyItem>();
    int m_FlyItemIndexCounter = 0;
    float m_TurnSpeed = 90;
    public void SaveIcon()
    {

    }
    public FlyItem AddFlyItem(FlyItem item = null)
    {
        if (item == null)
            item = CreateFlyItem();
        if (item != null)
        {
            m_FlyItems.Add(item);
            if (item.flyItemID == 0)
                item.flyItemID = ++m_FlyItemIndexCounter;
            if(item.iconPath == "" && typeID != 0)
            {
                item.iconPath = "Textures/Items/Weapon/Bullet/"+typeID+"_"+ item.flyItemID+".png";
            }
        }
        return item;
    }
    public FlyItem GetSelectedFlyItem()
    {
        if (flyItems.Count > 0)
            return flyItems[0];
        return null;
    }
    public void Clear()
    {
        iconPath = "";
        description = "";
        typeID = 0;
        name = "";
        ClearFlyItems();
    }
    public void ClearFlyItems()
    {
        m_FlyItems.Clear();
    }
    public override JsonData SavePrefab(JsonData json)
    {
        JsonData root = base.SavePrefab(json);
        root["FlyItemIndexCount"] = m_FlyItemIndexCounter;
        root["PivotX"] = m_Pivot.x;
        root["PivotY"] = m_Pivot.y;
        root["Level"] = (int)m_Level;

        var jsonFlyItems = new JsonData();
        jsonFlyItems.SetJsonType(JsonType.Array);
        foreach(var i in m_FlyItems)
        {
            jsonFlyItems.Add(i.Save());
        }
        root["FlyItems"] = jsonFlyItems;
        return root;
    }
    public override void LoadPrefab(JsonData json)
    {
        base.LoadPrefab(json) ;
        
        IDictionary<string, JsonData> jsond = json.ValueAsObject();
        if (jsond.ContainsKey("FlyItemIndexCount"))
            m_FlyItemIndexCounter = jsond["FlyItemIndexCount"].ValueAsInt();

        Vector2 pivot = new Vector2(0.5f, 0.5f);
        if (jsond.ContainsKey("PivotX"))
            pivot.x = (float)jsond["PivotX"].ValueAsDouble();
        if (jsond.ContainsKey("PivotY"))
            pivot.y = (float)jsond["PivotY"].ValueAsDouble();
        if (jsond.ContainsKey("Level"))
            m_Level = (Level)jsond["Level"].ValueAsInt();
        m_Pivot = pivot;

        m_FlyItems.Clear();
        if (jsond.ContainsKey("FlyItems"))
        {
            var jsondFlyItems = jsond["FlyItems"].ValueAsArray();
            foreach(var i in jsondFlyItems)
            {
                var flyItem = CreateFlyItem();
                flyItem.Load(i);
                m_FlyItems.Add(flyItem);
            }
        }

        return ;
    }

    public virtual FlyItem CreateFlyItem()
    {
        return null;
    }
    public override void Copy(Item item)
    {
        base.Copy(item);

        Weapon weapon = item as Weapon;
        m_FitSlotTypeID = weapon.fitSlotTypeID;
        pivot = weapon.pivot;
        m_FlyItems = weapon.flyItems;
        level = weapon.level;

    //public int fitSlotTypeID { get { return m_FitSlotTypeID; } }
    //public Vector2 pivot { get { return m_Pivot; } set { m_Pivot = value; } }
    //public List<FlyItem> flyItems { get { return m_FlyItems; } }
    //public Level level { get { return m_Level; } set { m_Level = value; } }
    //public Level m_Level;
    //int m_FitSlotTypeID = (int)ItemType.WEAPON_SLOT;
    //Vector2 m_Pivot = new Vector2(0.5f, 0.5f);
    //List<FlyItem> m_FlyItems = new List<FlyItem>();
    //int m_FlyItemIndexCounter = 0;
    }
}
